package com.yingside.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestCalculateFrame extends JFrame {


    private JTextField txtInput;
    private JPanel panel;
    private double v1,v2;
    private String operator;
    private boolean flag;

    public TestCalculateFrame(){
        this.setTitle("表格布局窗口");
        this.setBounds(300,300,400,400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        txtInput = new JTextField();
        //水平对齐方式
        txtInput.setHorizontalAlignment(JTextField.RIGHT);
        //不可修改
        //txtInput.setEditable(false);

        panel = new JPanel();
        panel.setLayout(new GridLayout(4,4,5,5));

        panel.add(getButton("7"));
        panel.add(getButton("8"));
        panel.add(getButton("9"));
        panel.add(getButton("/"));

        panel.add(getButton("4"));
        panel.add(getButton("5"));
        panel.add(getButton("6"));
        panel.add(getButton("*"));

        panel.add(getButton("1"));
        panel.add(getButton("2"));
        panel.add(getButton("3"));
        panel.add(getButton("+"));

        panel.add(getButton("0"));
        panel.add(getButton("."));
        panel.add(getButton("-"));
        panel.add(getButton("="));

        this.add(txtInput,BorderLayout.NORTH);
        this.add(panel);

    }

    public JButton getButton(String text){
        JButton btn = new JButton(text);

        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                doAction(text);

            }
        });

        return btn;
    }

    private void doAction(String text){

        if(flag){
            txtInput.setText("");
            flag = false;
        }

        switch (text){
            case ".":
            case "0":
            case "1":
            case "2":
            case "3":
            case "4":
            case "5":
            case "6":
            case "7":
            case "8":
            case "9":
                if("".equals(txtInput.getText()) && text.equals("0")){
                    return;
                }
                if("".equals(txtInput.getText()) && text.equals(".")){
                    txtInput.setText("0.");
                    return;
                }
                if(txtInput.getText().indexOf(".") != -1 && text.equals(".")){
                    return;
                }
                txtInput.setText(txtInput.getText() + text);
                break;
            case "+":
                v1 = Double.parseDouble(txtInput.getText());
                operator = "+";
                txtInput.setText("");
                break;
            case "-":
                break;
            case "*":
                break;
            case "/":
                break;
            case "=":

                v2 = Double.parseDouble(txtInput.getText());

                switch (operator){
                    case "+":
                        txtInput.setText(Double.toString(v1 + v2));
                        break;
                }

                flag = true;

                break;
        }
    }

    public static void main(String[] args) {
        TestCalculateFrame tcl = new TestCalculateFrame();
        tcl.setVisible(true);
    }
}
